package com.example.guethe.fireanalysisapp.activities;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;
import com.example.guethe.fireanalysisapp.R;
import com.example.guethe.fireanalysisapp.data.beans.BaseResponse;
import com.example.guethe.fireanalysisapp.data.beans.User;
import com.example.guethe.fireanalysisapp.data.net.Api;
import com.example.guethe.fireanalysisapp.util.ToastHelper;

import cn.droidlover.xdroidmvp.kit.Codec;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;

import static com.example.guethe.fireanalysisapp.util.MD5Util.getMD5;


public class MainActivity extends AppCompatActivity {

    private MapView mMapView = null;
    private BaiduMap baiduMap=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        mMapView = (MapView) findViewById(R.id.bmapView);
        //定义Maker坐标点

        baiduMap=mMapView.getMap();
        LatLng point = new LatLng(39.963175, 116.400244);


//构建Marker图标

        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.maker);

//构建MarkerOption，用于在地图上添加Marker

        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap);

//在地图上添加Marker，并显示

        baiduMap.addOverlay(option);
        baiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                TextView location = new TextView(getApplicationContext());
                location.setBackgroundResource(R.drawable.popup);
                location.setText("39.963175, 116.400244");


                location.setPadding(30, 20, 30, 50);
                //定义用于显示该InfoWindow的坐标点
                LatLng pt = new LatLng(39.963175, 116.400244);

                //创建InfoWindow , 传入 view， 地理坐标， y 轴偏移量
                InfoWindow mInfoWindow = new InfoWindow(location, pt, -47);

                //在地图上添加该文字对象并显示
                baiduMap.showInfoWindow(mInfoWindow);

                return false;
            }
        });
        User user=new User();
        user.setName("guest");
        user.setPassword(getMD5("123456"));
        Api.getBaseService().login(user)
                .compose(XApi.<BaseResponse<User>>getApiTransformer())
                .compose(XApi.<BaseResponse<User>>getScheduler())
                .subscribe(new ApiSubscriber<BaseResponse<User>>() {
                    @Override
                    protected void onFail(NetError error) {
                        ToastHelper.show("登录失败："+error.getMessage());
                    }

                    @Override
                    public void onNext(BaseResponse<User> userBaseResponse) {
                        ToastHelper.show("登录"+userBaseResponse.data.getName()+userBaseResponse.code);
                    }
                });
    }
}
